import static com.googlecode.javacv.cpp.opencv_core.cvGetSize;
import static com.googlecode.javacv.cpp.opencv_highgui.CV_LOAD_IMAGE_COLOR;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_BGR2HSV;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_COMP_BHATTACHARYYA;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_HIST_ARRAY;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_THRESH_BINARY;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvCalcHist;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvCompareHist;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvCreateHist;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvCvtColor;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvThreshold;
import static opencvtest.OpenCVUtils.loadAndShowOrExit;
import static opencvtest.OpenCVUtils.show;

import java.awt.image.BufferedImage;
import java.io.File;

import opencvtest.chapter04.ColorHistogram;
import opencvtest.chapter04.Histogram1D;

import com.googlecode.javacv.cpp.opencv_core.IplImage;
import com.googlecode.javacv.cpp.opencv_imgproc.CvHistogram;

/**    
 * 文件名：JavaCvHsvMatch.java    
 *    
 * 版本信息：    
 * 日期：2014年3月13日    
 * xyj 足下 xyj 2014     
 * 版权所有    
 *    
 */

/**
 * @项目名称：opencv-test
 * @类名称：JavaCvHsvMatch
 * @类描述：
 * @创建人：zhuyi
 * @创建时间：2014年3月13日 上午9:21:27
 * @修改人：zhuyi
 * @修改时间：2014年3月13日 上午9:21:27
 * @修改备注：
 * @version
 * 
 */
public class JavaCvHsvMatch {

    public static void main(String[] args) {

        IplImage image = loadAndShowOrExit(new File("E:/image-test/f1.jpg"), CV_LOAD_IMAGE_COLOR);
        IplImage image2 = loadAndShowOrExit(new File("E:/image-test/f2.jpg"), CV_LOAD_IMAGE_COLOR);

        IplImage hsvTargetImage = IplImage.create(cvGetSize(image), image.depth(), 3);
        cvCvtColor(image, hsvTargetImage, CV_BGR2HSV);

        IplImage hsvTargetImage2 = IplImage.create(cvGetSize(image2), image2.depth(), 3);
        cvCvtColor(image2, hsvTargetImage2, CV_BGR2HSV);

        IplImage saturationChannel = ColorHistogram.splitChannels(hsvTargetImage)[1];
        IplImage saturationChannel2 = ColorHistogram.splitChannels(hsvTargetImage2)[1];

        int minSaturation = 65;
        cvThreshold(saturationChannel, saturationChannel, minSaturation, 255, CV_THRESH_BINARY);
        cvThreshold(saturationChannel2, saturationChannel2, minSaturation, 255, CV_THRESH_BINARY);

        show(saturationChannel, "hsv1");
        show(saturationChannel2, "hsv2");

        int histSize = 256;
        float[] range = { 0, 255 };
        float[][] ranges = { range };

        CvHistogram hist = cvCreateHist(1, new int[] { histSize }, CV_HIST_ARRAY, ranges, 1);
        cvCalcHist(new IplImage[] { saturationChannel }, hist, 0, null);
        BufferedImage histImg = new Histogram1D().getHistogram(hist);
        show(histImg, "saturation channel 1");

        CvHistogram hist2 = cvCreateHist(1, new int[] { histSize }, CV_HIST_ARRAY, ranges, 1);
        cvCalcHist(new IplImage[] { saturationChannel }, hist2, 0, null);
        BufferedImage histImg2 = new Histogram1D().getHistogram(hist2);
        show(histImg2, "saturation channel 2");

        double com = cvCompareHist(hist, hist2, CV_COMP_BHATTACHARYYA);
        System.out.println(com);

    }

}
